import { reqGetUserAddress, reqGetOrder, reqSubmitOrder } from "@/api";

const state = {
  userAddress: [],
  userOrder: {},
};

const mutations = {
  USERADDRESS(store, data) {
    store.userAddress = data;
  },
  USERORDER(store, data) {
    store.userOrder = data;
  },
};

const actions = {
  async getUserAddress({ commit }) {
    const { data } = await reqGetUserAddress();
    if (data.code == 200) {
      commit("USERADDRESS", data.data);
      return "ok";
    } else {
      return Promise.reject(new Error("fail"));
    }
  },
  async getOrder({ commit }) {
    const { data } = await reqGetOrder();
    commit("USERORDER", data.data);
  },
  async submitOrder({ commit }, { obj, tradeNo }) {
    const { data } = await reqSubmitOrder(obj, tradeNo);
    if (data.code == 200) {
      return data;
    } else {
      return Promise.reject(new Error("fail"));
    }
  },
};

const getters = {};

export default {
  state,
  mutations,
  actions,
  getters,
};
